/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _TANSU_UTIL_H_
#define _TANSU_UTIL_H_

#include "tansu_status.h"

/*
 * Additional STAILQ routines.
 */

#define STAILQ_MOVE(var, head, from, field) do { \
    while ((var = STAILQ_FIRST(from))) { \
        STAILQ_REMOVE_HEAD(from, field); \
        STAILQ_INSERT_TAIL(head, var, field); \
    } \
} while (/*CONSTCOND*/0)

#define STAILQ_FREE_ALL(var, head, field, func) do { \
    while ((var = STAILQ_FIRST(head))) { \
        STAILQ_REMOVE_HEAD(head, field); \
        func(var); \
    } \
} while (/*CONSTCOND*/0)

#define TAILQ_FREE_ALL(var, head, field, func) do { \
    while ((var = TAILQ_FIRST(head))) { \
        TAILQ_REMOVE(head, var, field); \
        func(var); \
    } \
} while (/*CONSTCOND*/0)

#define TANSU_SET_STATUS(status, value) do { \
    if (status) { \
        (*status) = value; \
    } \
} while (/*CONSTCOND*/0)

#define TAILQ_FOREACH_2(var1, var2, head1, head2, field) \
    for ((var1) = TAILQ_FIRST((head1)), (var2) = TAILQ_FIRST((head2)); \
         (var1) || (var2); \
         (var1) = (var1) ? TAILQ_NEXT((var1), field) : NULL, \
         (var2) = (var2) ? TAILQ_NEXT((var2), field) : NULL)

#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

#ifdef __cplusplus
extern "C" {
#endif

struct tansu_keyval {
    STAILQ_ENTRY(tansu_keyval) next;
    char *key;
    char *value;
};

STAILQ_HEAD(tansu_keyvals, tansu_keyval);

struct tansu_keyval *tansu_keyval_new(const char *key, const char *value);
void tansu_keyval_delete(struct tansu_keyval *keyval);

tansu_status_t
tansu_keyvals_append(struct tansu_keyvals *head,
                     const char *key,
                     const char *value);

/**
 * Appends or updates a key-value pair.
 *
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_MEMORY_ERROR
 */
tansu_status_t
tansu_keyvals_add(struct tansu_keyvals *head,
                  const char *name,
                  const char *value);

/**
 * Removes the specified key-value pair.
 *
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_NOT_FOUND
 */
tansu_status_t
tansu_keyvals_remove(struct tansu_keyvals *head, const char *name);

struct tansu_keyval *
tansu_keyvals_get(const struct tansu_keyvals *head, const char *name);

tansu_status_t
tansu_keyvals_copy(struct tansu_keyvals *dest, const struct tansu_keyvals *src);

void
tansu_keyvals_clear(struct tansu_keyvals *head);


char *tansu_strndup(const char *s, uint32_t n);

char *tansu_buffer_readline(char *buff, char **next);

const char *tansu_skip_space(const char *s);

/**
 * @retval  0 success
 * @retval -1 failure
 */
int tansu_time_get_monotonic(struct timeval *tv);
int tansu_time_get_timeout(struct timeval *tv, const struct timeval *target);

#define tansu_time_clear(tv) do { \
    (tv)->tv_sec = 0; \
    (tv)->tv_usec = 0; \
} while (0)

#define tansu_time_sub(tv1, tv2) do { \
    (tv1)->tv_sec -= (tv2)->tv_sec; \
    (tv1)->tv_usec -= (tv2)->tv_usec; \
    if ((tv1)->tv_usec < 0) { \
        (tv1)->tv_sec -= 1; \
        (tv1)->tv_usec += (1000 * 1000); \
    } \
} while (0);

#define tansu_time_add_msec(tv, msec) do { \
    (tv)->tv_sec += (msec) / 1000; \
    (tv)->tv_usec += ((msec) % 1000) * 1000; \
    if ((tv)->tv_usec > (1000 * 1000)) { \
        (tv)->tv_sec += 1; \
        (tv)->tv_usec -= (1000 * 1000); \
    } \
} while (0);

int tansu_time_cmp(const struct timeval *tv1, const struct timeval *tv2);

int tansu_get_gmt_iso1123(char *buff, int size);

uint16_t tansu_url_getport(const char *url);
const char *tansu_url_getpath(const char *url);

int tansu_url_isaddr(const char *url, const struct sockaddr_storage *ss);

tansu_status_t
tansu_url_parse(const char *url,
                char **scheme,
                char **user,
                char **pass,
                char **address,
                uint16_t *port,
                const char **path);

#ifdef __cplusplus
}
#endif

#endif /* _TANSU_UTIL_H_ */

